#include <iostream>
#include <map>
#include <string>
#include <utility>

using std::cout;
using std::endl;
using std::map;
using std::string;
using std::pair;
using std::make_pair;

void test()
{
    //map的特征
    //1、会按照key-value存放数据，key值是唯一的，不能重复
    //value值是可以重复，也可以不重复
    //2、默认情况会按照key值升序排列
    //3、底层使用的也是红黑树
    map<int, string> number = {
        {1, "wuhan"},
        {5, "beijing"},
        {9, "nanjing"},
        pair<int, string>(3, "shanghai"),
        pair<int, string>(5, "shanghai"),
        pair<int, string>(3, "shanghai"),
        make_pair(2, "hubei"),
        make_pair(10, "hubei"),
        make_pair(14, "dongjing")
    };

    for(auto &elem : number)
    {
        cout << elem.first << "    " << elem.second << endl;
    }

    cout << endl << "map的insert操作" << endl;
    pair<map<int, string>::iterator, bool> ret 
        /* = number.insert({7,"wangdao"}); */
        /* = number.insert(pair<int, string>(7,"wangdao")); */
        = number.insert(make_pair(7,"wangdao"));
    if(ret.second)
    {
        cout << "插入成功 " << ret.first->first 
             << "    " << ret.first->second << endl;
    }
    else
    {
        cout << "插入失败，该元素存在map中" << endl;
    }
    for(auto &elem : number)
    {
        cout << elem.first << "    " << elem.second << endl;
    }

    cout << endl << "map的下标访问" << endl;
    cout << "number[3] = " << number[3] << endl;//查找
    cout << "number[4] = " << number[4] << endl;//插入
    for(auto &elem : number)
    {
        cout << elem.first << "    " << elem.second << endl;
    }

    cout << endl <<endl;
    number[4] = "tianjin";//修改
    number[10] = "tianjin";//修改
    for(auto &elem : number)
    {
        cout << elem.first << "    " << elem.second << endl;
    }
}

int main(int argc, char *argv[])
{
    test();
    return 0;
}

